﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TETRIS_v01.GameHost;
using System.Threading;
using TETRIS_v01.GameHost.GameSounds;

namespace TETRIS_v01.FieldObjects
{    
    public class InfoBoard
    {   
        public int GameLevel { get; protected set; }
        public int LevelStep { get; protected set; }
        public int GameSpeed { get; protected set; }
        public int RowsKilled { get; protected set; }
        public int FiguresFallen { get; protected set; }
        public int GameScore { get; protected set; }
        public string PauseHelp { get; protected set; }
        public Coordinates[] FigureTablo { get; protected set; }
        public Coordinates[] EventTablo { get; protected set; }
        public Field FieldClient { get; protected set; }
        int startLevel;//сделать входящим параметром
        SoundPlayer player = new SoundPlayer();

        public InfoBoard(Field f)
        {
            FieldClient = f;
            startLevel = 1; 
            GameLevel = startLevel;
            LevelStep = 10;            
            GameSpeed = ChooseGameSpeed();
            RowsKilled = 0;
            FiguresFallen = 0;
            GameScore = 0;
            PauseHelp = "Press <ESC> to Pause/Continue";
            
            
            FigureTablo = new Coordinates [24]; //4*6
            int i = 0;
            for (int y = f.StartPoint.y + 3; y < f.StartPoint.y + 9; y++)
            {
                for (int x = f.StartPoint.x + f.FieldWidth + 7; x <f.StartPoint.x + f.FieldWidth+11; x++)
                {
                    FigureTablo[i] = new Coordinates(x, y);
                    i++;
                }
            }

            EventTablo = new Coordinates[55]; //11*5
            int j = 0;
            for (int y = f.StartPoint.y + 14; y < f.StartPoint.y + 19; y++)
            {
                for (int x = f.StartPoint.x + f.FieldWidth + 5; x < f.StartPoint.x + f.FieldWidth + 16; x++)
                {
                    EventTablo[j] = new Coordinates(x, y);
                    j++;
                }
            }
        }

        public void ClearEvents()
        {
            foreach (Coordinates c in EventTablo)
            {
                c.Clear(c.x, c.y);
            }
        }

        private void ShowNextShapes(Shape [] sh)
        {
            foreach (Coordinates c in FigureTablo)
            {
                c.Clear(c.x, c.y);
            }

            sh[2].MoveRight(FieldClient.FieldWidth + 23); sh[2].MoveBottom(4);
            sh[2].PrintShape();
            sh[2].MoveLeft(FieldClient.FieldWidth + 23); sh[2].MoveBottom(-4);

            sh[1].MoveRight(FieldClient.FieldWidth + 23); sh[1].MoveBottom(8);
            sh[1].PrintShape();
            sh[1].MoveLeft(FieldClient.FieldWidth + 23); sh[1].MoveBottom(-8);
        }

        private void SendEvent(string event1, string event2, string event3, string event4)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(FieldClient.StartPoint.x + FieldClient.FieldWidth + 5, FieldClient.StartPoint.y + 14);
            Console.Write("{0}", event1);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(FieldClient.StartPoint.x + FieldClient.FieldWidth + 5, FieldClient.StartPoint.y + 16);
            Console.Write("{0}", event2);
            Console.SetCursorPosition(FieldClient.StartPoint.x + FieldClient.FieldWidth + 5, FieldClient.StartPoint.y + 17);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("{0}", event3);            
            Console.SetCursorPosition(FieldClient.StartPoint.x + FieldClient.FieldWidth + 5, FieldClient.StartPoint.y + 18);
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("{0}", event4);
            Console.ResetColor();            
        }

        public void RefreshBoard(int newRowsKilled, int figureCounter, Shape[] shapeStack)
        {
            RowsKilled = RowsKilled + newRowsKilled;
            FiguresFallen = figureCounter;
            
            int rowsPoints = 0;
            int figurePoints = 1*GameLevel;
            switch (newRowsKilled)
            {
                case 1:
                    rowsPoints = 10*GameLevel; SendEvent("","",string.Format("+{0}",rowsPoints),string.Format("+{0}",figurePoints)); break;                    
                case 2:
                    rowsPoints = 30*GameLevel; SendEvent("","DOUBLE",string.Format("+{0}",rowsPoints),string.Format("+{0}",figurePoints)); break;
                case 3:
                    rowsPoints = 50*GameLevel; SendEvent("","TRIPPLE",string.Format("+{0}",rowsPoints),string.Format("+{0}",figurePoints)); break;
                case 4:
                    rowsPoints = 70*GameLevel; SendEvent("Oh, sheat!","QUADRUPLE",string.Format("+{0}",rowsPoints),string.Format("+{0}",figurePoints)); break;
                default:
                    if(figureCounter>0){SendEvent("           ", "", "", string.Format("+{0}",figurePoints));} break;
            }
            if (figureCounter == 0) { SendEvent("START !", "", "", ""); }
            else                    { GameScore = GameScore + rowsPoints + figurePoints; }
            
            ShowNextShapes(shapeStack);

            int NextGameLevel = Math.Min(10, startLevel+(int)Math.Floor((decimal)RowsKilled / LevelStep));
            if (NextGameLevel > GameLevel)
            {
                GameLevel = NextGameLevel;
                GameSpeed = ChooseGameSpeed();
                if (NextGameLevel == 10) { SendEvent("FINAL LEVEL", "", "", "");  }
                else
                {
                    SendEvent("LEVEL UP !", "", "", "");
                    //player.playSound("D:\\Sounds\\applause.wav");
                }
            }            
        }

        private int ChooseGameSpeed()
        {
            int speed=0;
            switch (GameLevel)
            {
                case 1: speed = 600; break;
                case 2: speed = 500; break;
                case 3: speed = 420; break;
                case 4: speed = 350; break;
                case 5: speed = 300; break;
                case 6: speed = 260; break;
                case 7: speed = 220; break;
                case 8: speed = 190; break;
                case 9: speed = 160; break;
                case 10: speed = 130; break;
            }
            return speed;
        }

        public void DrawInfoBoard(Field f)
        {
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.SetCursorPosition(3, f.StartPoint.y + 1); Console.Write("[ LEVEL ]");
            Console.SetCursorPosition(3, f.StartPoint.y + 7); Console.Write("[ SCORE ]");

            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.SetCursorPosition(3, f.StartPoint.y + 16); Console.Write(" ROWS    ");
            Console.SetCursorPosition(3, f.StartPoint.y + 22); Console.Write(" FIGURES ");

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.SetCursorPosition(5, f.StartPoint.y + 3);
            Console.Write("{0}", GameLevel);
            Console.SetCursorPosition(5, f.StartPoint.y + 9);
            Console.Write("{0}", GameScore);

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.SetCursorPosition(5, f.StartPoint.y + 18);
            Console.Write("{0}", RowsKilled);
            Console.SetCursorPosition(5, f.StartPoint.y + 24);
            Console.Write("{0}", FiguresFallen);

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.SetCursorPosition(f.StartPoint.x + f.FieldWidth + 5, f.StartPoint.y + 1);
            Console.Write("  NEXT   ");
            
            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.SetCursorPosition(0, f.StartPoint.y + f.FieldHeight + 4);
            for (int i = 0; i < f.FieldWidth + 34; i++)
            {
                Console.Write(" ");
            }
            Console.SetCursorPosition((f.FieldWidth + 34 - PauseHelp.Length) / 2, f.StartPoint.y + f.FieldHeight + 4);
            Console.Write("{0}", PauseHelp);
            Console.ResetColor();
        }
    }
}
